package com.gongyou.forum.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gongyou.common.pojo.Topic;
import com.gongyou.common.utils.DateUtils;
import com.gongyou.common.vo.CircleVo;
import com.gongyou.forum.mapper.CircleMapper;
import com.gongyou.forum.service.CircleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author LiQuanPing
 * @Date 2021-03-19 16:40
 * @Version 1.8
 * @creed: ♂♂Talk is cheap,show me the code♀♀
 */
@Service
public class CircleServiceImpl implements CircleService {

    @Autowired
    private CircleMapper circleMapper;

    //读取圈子所有列表
    @Override
    public List getCircle(String userId, String pageNum) {
        long size = Long.parseLong(pageNum);
        Page page = new Page(0,size*10);
       // List<Map> list = circleMapper.getCircle(page,userId);
        Page<Map> page1 = circleMapper.getCircle(page,userId);
       // System.out.println("===="+list);
        List<Map> records = page1.getRecords();
        List<CircleVo> circleList = new ArrayList();
        for (Map map : records) {
            CircleVo circleVo = JSON.parseObject(JSON.toJSONString(map), CircleVo.class);
            circleList.add(circleVo);
        }

        for (CircleVo topic : circleList) {
            String url = topic.getUrl();
            // String[] split = url.split(";");
            List list1 = JSON.parseObject(url, List.class);
            topic.setPocUrl(list1);
            String reqDate = DateUtils.mill2String(topic.getRepDate());
            String pubDate = DateUtils.mill2String(topic.getPubDate());
            topic.setRepDate(reqDate);
            topic.setPubDate(pubDate);
        }
        return circleList;
    }

    //读取圈子总数
    @Override
    public Integer getCircleCount() {
        return circleMapper.selectCount(null);

    }

    //发布圈子动态,保存发布的信息
    @Override
    public void publishCircle(Topic topic) {
        List pocUrl = topic.getPocUrl();
        String url = JSON.toJSONString(pocUrl);
        topic.setUrl(url);
        int insert = circleMapper.insert(topic);
        System.out.println(insert + "]]]]]]]]]]");


    }

    public static void main(String[] args) {

        String s = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.win4000.com%2Fpic%2F2%2F0e%2F301c33c393.jpg%3Fdown&refer=http%3A%2F%2Fpic1.win4000.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1618751076&t=eec1458b592cd9cb02312dc876052097;ewew";

        String[] split = s.split(";");


        System.out.println(Arrays.toString(split));
    }
}
